<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div class="logo-box">
        <img src="@/assets/images/logo.png" class="logo" />
        <p class="title">中国人民解放军八一电影制片厂管理系统</p>
      </div>
      <el-button type="info" @click="loginOut">退出</el-button>
    </el-header>
    <!-- 页面主体区 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="asideWith">
        <!-- unique-opened：只保持一个子菜单的展开
        background-color：设置菜单的背景色
        text-color：设置菜单文本显示颜色
         -->
        <div class="btn" @click="isCollapse = !isCollapse">
          |||
        </div>
        <el-menu
          unique-opened
          background-color="#333744"
          text-color="#fff"
          :collapse="isCollapse"
          :collapse-transition="false"
          router
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="menu.path"
            v-for="menu in menuList"
            :key="menu.id"
          >
            <template slot="title">
              <!-- 一级菜单图标 -->
              <i :class="`iconfont ${iconMap[menu.id]}`"></i>
              <!-- 一级菜单的文本 -->
              <span>{{ menu.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="subMenu.path"
              v-for="subMenu in menu.children"
              :key="subMenu.id"
            >
              <template slot="title">
                <!-- 二级菜单图标 -->
                <i class="el-icon-menu"></i>
                <!-- 二级菜单的文本 -->
                <span>{{ subMenu.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
import { storageClearAll } from '@/storage'
import { getMenuList } from '@/api/power'
export default {
  name: 'Home',
  data() {
    return {
      iconMap: {
        // 自已去组织一个映射关系（菜单id：icon类名）
        125: 'icon-users',
        103: 'icon-tijikongjian',
        101: 'icon-shangpin',
        102: 'icon-danju',
        145: 'icon-baobiao'
      },
      isCollapse: false,
      menuList: []
    }
  },
  computed: {
    asideWith() {
      if (this.isCollapse) {
        return '64px'
      } else {
        return '200px'
      }
    }
  },
  methods: {
    // 按钮功能
    loginOut() {
      // 清除本地token
      // localStorage.clear()
      storageClearAll()
      // 跳转到登录
      this.$router.push('/login')
    },
    async loadMenuList() {
      const res = await getMenuList()
      console.log(res)
      this.menuList = res.data
    }
  },
  mounted() {
    // 从当前页面加载完毕之后，请求菜单数据
    this.loadMenuList()
  }
}
</script>

<style scoped lang="less">
.el-container {
  height: 100%;
  .el-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background-color: #373d41;
  }
  .logo-box {
    display: flex;
    align-items: center;
  }
  .logo {
    width: 40px;
    margin-right: 10px;
  }
  .title {
    font-size: 20px;
    color: #fff;
  }
}

.el-aside {
  background-color: #333744;
  .btn {
    background-color: #4a5064;
    height: 30px;
    color: #fff;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
  }
  .el-menu {
    border: none;
  }
  .iconfont {
    margin-right: 10px;
  }
}

.el-main {
  background-color: #eaedf1;
}
</style>
